package com.wwl.cms.config.elasticsearch;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wwl.cms.dto.UserDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
/*
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
*/
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/10/22 14:50
 * @description:采用REST客户端对象
 */
@Slf4j
public class EsConfig {
    public static void main(String[] args) {
        //创建客户端
        try {
           /* RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );*/
            //1.索引操作
            //创建索引
           /* CreateIndexRequest request = new CreateIndexRequest("user");
            CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
            boolean acknowledged = response.isAcknowledged();
            log.info("操作状态：{}",acknowledged);*/

            //查看索引
            /*GetIndexRequest request = new GetIndexRequest("user");
            GetIndexResponse response = client.indices().get(request,RequestOptions.DEFAULT);//发送请求，获取响应
            log.info("aliases:{}",response.getAliases());
            log.info("mappings:{}",response.getMappings());
            log.info("settings:{}",response.getSettings());*/

            //删除索引
           /* DeleteIndexRequest request = new DeleteIndexRequest("user");
            AcknowledgedResponse response = client.indices().delete(request,RequestOptions.DEFAULT);
            log.info("操作结果：{}",response.isAcknowledged());*/

            //2.文档操作
            //创建数据添加文档
           /* IndexRequest request = new IndexRequest();
            request.index("user").id("1003");//设置索引及唯一标识
            UserDto user = new UserDto();
            user.setName("张三");
            user.setAge(18);
            user.setSex("男");
            ObjectMapper objectMapper = new ObjectMapper();
            String userString = objectMapper.writeValueAsString(user);
            request.source(userString, XContentType.JSON);//添加文档数据,数据格式为json
            IndexResponse response = client.index(request,RequestOptions.DEFAULT);
            log.info("_index:{};_id:{};_result:{};",response.getIndex(),response.getId(),response.getResult());*/

           //修改文档
            /*UpdateRequest request = new UpdateRequest();
            request.index("user").id("1003");
            request.doc(XContentType.JSON,"sex","女");
            UpdateResponse response = client.update(request,RequestOptions.DEFAULT);
            log.info("_index:{};_id:{};_result:{};",response.getIndex(),response.getId(),response.getResult());*/

            //查询文档
           /* GetRequest request = new GetRequest().index("user").id("1003");
            GetResponse response = client.get(request,RequestOptions.DEFAULT);
            log.info("_index:{};_type:{};_id:{};_source:{};",response.getIndex(),response.getType(),response.getId(),response.getSourceAsString());*/

            //删除文档
           /* DeleteRequest request = new DeleteRequest().index("user").id("1001");
            DeleteResponse response = client.delete(request,RequestOptions.DEFAULT);
            log.info(response.toString());*/

           //批量操作
            //批量新增
            BulkRequest request = new BulkRequest();
            request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON,"name","zhangsan1","age","18","sex","男"));
            request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON,"name","2zhangsan","age","18","sex","男"));
            request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON,"name","zhangsan12","age","18","sex","男"));
            /*request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON,"name","lisi","age","19","sex","女"));
            request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON,"name","wangwu","age","20","sex","男"));*/
            //BulkResponse responses = client.bulk(request,RequestOptions.DEFAULT);
           // log.info("took:{};items:{}",responses.getTook(),responses.getItems());
            //批量删除
            /*BulkRequest request = new BulkRequest();
            request.add(new DeleteRequest().index("user").id("1001"));
            request.add(new DeleteRequest().index("user").id("1002"));
            request.add(new DeleteRequest().index("user").id("1003"));
            BulkResponse responses = client.bulk(request,RequestOptions.DEFAULT);
            log.info("took:{};items:{}",responses.getTook(), JSON.toJSONString(responses.getItems()));*/
            //client.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
